package hibernate.xml43.view;

import hibernate.xml43.pojo.Dept;
import hibernate.xml43.util.HibernateHandler;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class View5 {
	public static void main(String[] args) {
		//Criteria查询
		//完全面向对象,非常方便,但是我们一般还是常用HQL
		Session session = HibernateHandler.getSession();
		//按deptno降序查询dname中还有x的前三条记录
		Criteria criteria = session.createCriteria(Dept.class)
		.add(Restrictions.ilike("dname", "%x%"))
		.setMaxResults(3).addOrder(Order.desc("deptno"));
		@SuppressWarnings("unchecked")
		List<Dept> deptList = criteria.list();
		for(Dept d : deptList){
			System.out.print("id:"+d.getDeptno()+",");
			System.out.print("dname:"+d.getDname()+",");
			System.out.println("dname:"+d.getLoc());
		}
		session.close();
	}
}
